# ffos makefile
# 2020, Simon Zolin

ROOT := ../..
FFOS_DIR := $(ROOT)/ffos
FFBASE_DIR := $(ROOT)/ffbase
HEADERS := $(wildcard $(FFOS_DIR)/FFOS/*.h)

include $(FFBASE_DIR)/test/makeconf

TEST_EXE := ffostest$(DOTEXE)

TEST_OBJ := \
	backtrace.o \
	compat.o \
	dir.o \
	dylib.o \
	environ.o \
	error.o \
	file.o \
	filemap.o \
	kcall.o \
	kqueue.o \
	mem.o \
	path.o \
	perf.o \
	pipe.o \
	process.o \
	semaphore.o \
	signal.o \
	socket.o \
	std.o \
	test.o \
	thread.o \
	timer.o \
	timerqueue.o \
	vol.o
# 	fileaio.o
HEADERS += $(FFOS_DIR)/test/Makefile
ifeq "$(OS)" "windows"
	TEST_OBJ += \
		filemon.o \
		winreg.o
else
	ifeq ($(OS),linux)
		TEST_OBJ += \
			filemon.o \
			netlink.o
	else ifeq ($(OS),freebsd)
	else ifeq ($(OS),apple)
	endif
endif

all: $(TEST_EXE)

clean:
	$(RM) $(TEST_EXE) $(TEST_OBJ)

TEST_CFLAGS := -I$(FFOS_DIR) -I$(FFBASE_DIR) \
	-Wall -Wextra -Wno-array-bounds -fno-strict-aliasing \
	-DFFBASE_HAVE_FFERR_STR -DFF_DEBUG -g
ifeq "$(DEBUG)" "1"
	TEST_CFLAGS += -O0 -DFFBASE_DEBUG
else
	TEST_CFLAGS += -O3
endif
TEST_CXXFLAGS := $(TEST_CFLAGS)
TEST_CFLAGS += -std=gnu99
ifeq "$(ASAN)" "1"
	TEST_CFLAGS += -fsanitize=address
	TEST_LDFLAGS += -fsanitize=address
endif

ifeq "$(OS)" "windows"
	# TEST_CFLAGS += -DFF_WIN_APIVER=0x0501
	TEST_LDLIBS += -lws2_32 -liphlpapi
else
	TEST_LDLIBS += -pthread
	ifeq ($(OS),linux)
		TEST_LDLIBS += -ldl
	else ifeq ($(OS),freebsd)
		TEST_LDLIBS += -lexecinfo
	endif
endif

%.o: $(FFOS_DIR)/FFOS/%.c
	$(C) $(TEST_CFLAGS) $< -o $@

test.o: $(FFOS_DIR)/test/test.c $(FFOS_DIR)/test/tests.h $(HEADERS)
	$(C) $(TEST_CFLAGS) $< -o $@
%.o: $(FFOS_DIR)/test/%.c $(HEADERS)
	$(C) $(TEST_CFLAGS) $< -o $@

%.o: $(FFOS_DIR)/test/%.cpp $(HEADERS)
	$(CXX) $(TEST_CXXFLAGS) $< -o $@

$(TEST_EXE): $(TEST_OBJ)
	$(LINK) $(TEST_LDFLAGS) $+ $(TEST_LDLIBS) -o $@
